package com.example.lte.api;

import cn.hutool.extra.pinyin.engine.pinyin4j.Pinyin4jEngine;
import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiSnsGetuserinfoBycodeRequest;
import com.dingtalk.api.response.OapiSnsGetuserinfoBycodeResponse;
import com.example.lte.config.BusinessLogic;
import com.example.lte.config.SysBusinessUnitEnum;
import com.example.lte.entity.*;
import com.example.lte.po.LoginPO;
import com.example.lte.po.ReqDevice;
import com.example.lte.repo.*;
import com.example.lte.service.IUserService;
import com.example.lte.service.TestRetryService;
import com.example.lte.util.DeviceUtil;
import com.example.lte.util.IpUtils;
import com.taobao.api.ApiException;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


/**
 * @author lzh
 * @date 2020/10/19 - 10:21
 */
@RestController
@Slf4j
@Api(tags = "初始化模块")
public class TestController {

    @Autowired
    private TestRetryService testRetryService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private UserRepo userRepo;
    @Autowired
    private RoleRepo roleRepo;

    @Autowired
    private RoleUserRelRepo roleUserRelRepo;

    @Autowired
    private ButtonRepo buttonRepo;
    @Autowired
    private RoleButtonRelRepo roleButtonRelRepo;
    @Autowired
    private MenuRepo menuRepo;

    @Autowired
    private RoleMenuRelRepo roleMenuRelRepo;

    @Autowired
    private RestTemplate restTemplate;



    @GetMapping("oauthLogin")
    public LoginPO oauthLogin(@RequestParam String code, @RequestParam String state, HttpServletRequest request) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException, ApiException {


        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
        OapiSnsGetuserinfoBycodeRequest req = new OapiSnsGetuserinfoBycodeRequest();
        req.setTmpAuthCode(code);
        OapiSnsGetuserinfoBycodeResponse response = client.execute(req,"dingoajs62b97gksaysthw","q6NrQoaUtfMH5bOF8ZvYJXgPNt4sxHRdFKJAqFxl1m89dp3oKjbUxXkWmckeL66V");

        String res = JSON.toJSONString(response.getUserInfo());
        log.info("dd--res:{}",res);

        UserEntity  udb =userRepo.findByDdLoginCode(response.getUserInfo().getOpenid());
        if (udb == null){
            //钉钉第一次扫码，创建用户到本系统
            Pinyin4jEngine engine = new Pinyin4jEngine();
            String pinyinName= engine.getPinyin(response.getUserInfo().getNick(),"");
            UserEntity u = UserEntity.builder()
                    .account(pinyinName+System.currentTimeMillis())
                    .ddLoginCode(response.getUserInfo().getOpenid())
                    .name(response.getUserInfo().getNick())
                    .sex(1)
                    .password("10470C3B4B1FED12C3BAAC014BE15FAC67C6E815")
                    .build();
            udb = userRepo.save(u);
            roleUserRelRepo.save(RoleUserEntity.builder()
                    .roleId(roleRepo.findByName("play").getId())
                    .userId(udb.getId())
                    .build());
        }
        //不是第一次扫码,继续登录
        String jsmd5pwd ="e10adc3949ba59abbe56e057f20f883e";
        String clientIp = IpUtils.getIpAddr(request);
        ReqDevice reqDevice = DeviceUtil.getReqDevice(request);
        return iUserService.doLogin(udb.getAccount(), jsmd5pwd,clientIp,reqDevice);


    }


    @GetMapping("/testRetry")
    public UserEntity  queryByAccount(String account){
       return testRetryService.queryByAccount(account);
    }

    @GetMapping("init")
    @BusinessLogic(value = "数据初始化",businessUnit = SysBusinessUnitEnum.DEFAULT_UNIT)
    @Transactional(rollbackFor = Exception.class)
    public String t1() {
        UserEntity userDO = new UserEntity();

        userDO.setName("风清扬");
        userDO.setAccount("fengqy");
        userDO.setPassword("10470C3B4B1FED12C3BAAC014BE15FAC67C6E815");
        userDO.setSex(1);
        userRepo.save(userDO);

        userDO = new UserEntity();
        userDO.setName("东方不败");
        userDO.setSex(1);
        userDO.setAccount("bubai");
        userDO.setPassword("10470C3B4B1FED12C3BAAC014BE15FAC67C6E815");
        userRepo.save(userDO);

        userDO = new UserEntity();
        userDO.setName("向问天");
        userDO.setSex(1);
        userDO.setAccount("wentian");
        userDO.setPassword("10470C3B4B1FED12C3BAAC014BE15FAC67C6E815");
        userRepo.save(userDO);

        userDO = new UserEntity();
        userDO.setName("任盈盈");
        userDO.setSex(0);
        userDO.setAccount("ryy");
        userDO.setPassword("10470C3B4B1FED12C3BAAC014BE15FAC67C6E815");
        userRepo.save(userDO);

        userDO = new UserEntity();
        userDO.setName("令狐冲");
        userDO.setSex(1);

        userDO.setAccount("lhc");
        userDO.setPassword("10470C3B4B1FED12C3BAAC014BE15FAC67C6E815");
        userRepo.save(userDO);

        userDO = new UserEntity();
        userDO.setName("田伯光");
        userDO.setSex(1);
        userDO.setAccount("tbg");
        userDO.setPassword("10470C3B4B1FED12C3BAAC014BE15FAC67C6E815");
        userRepo.save(userDO);

        userDO = new UserEntity();

        userDO.setName("仪琳");
        userDO.setSex(0);
        userDO.setAccount("yl");
        userDO.setPassword("10470C3B4B1FED12C3BAAC014BE15FAC67C6E815");
        userRepo.save(userDO);

        userDO = new UserEntity();

        userDO.setName("风二中");
        userDO.setSex(1);
        userDO.setAccount("fzz");
        userDO.setPassword("10470C3B4B1FED12C3BAAC014BE15FAC67C6E815");
        userRepo.save(userDO);

        userDO = new UserEntity();

        userDO.setName("李风");
        userDO.setSex(1);
        userDO.setAccount("lf");
        userDO.setPassword("10470C3B4B1FED12C3BAAC014BE15FAC67C6E815");
        userRepo.save(userDO);

        userDO = new UserEntity();

        userDO.setName("风一");
        userDO.setSex(1);
        userDO.setAccount("f1");
        userDO.setPassword("10470C3B4B1FED12C3BAAC014BE15FAC67C6E815");
        userRepo.save(userDO);

        userDO = new UserEntity();

        userDO.setName("无二");
        userDO.setAccount("wr");
        userDO.setSex(1);
        userDO.setPassword("10470C3B4B1FED12C3BAAC014BE15FAC67C6E815");
        userRepo.save(userDO);

        roleRepo.save(RoleEntity.builder().name("dev").note("开发").build());
        roleRepo.save(RoleEntity.builder().name("test").note("测试").build());
        roleRepo.save(RoleEntity.builder().name("admin").note("管理员").build());
        roleRepo.save(RoleEntity.builder().name("u1").note("普通用户1").build());
        roleRepo.save(RoleEntity.builder().name("play").note("游客").build());


        roleUserRelRepo.save(RoleUserEntity.builder()
                .roleId(roleRepo.findByName("dev").getId())
                .userId(userRepo.findByAccount("lhc").getId())
                .build());
        roleUserRelRepo.save(RoleUserEntity.builder()
                .roleId(roleRepo.findByName("test").getId())
                .userId(userRepo.findByAccount("ryy").getId())
                .build());

        //---------------------------------------------------


        buttonRepo.save(ButtonEntity.builder()
                .buttonName("addUser")
                .note("新增用户")
                .build());
        buttonRepo.save(ButtonEntity.builder()
                .buttonName("editUser")
                .note("编辑用户")
                .build());
        buttonRepo.save(ButtonEntity.builder()
                .buttonName("deleteUser")
                .note("删除用户")
                .build());


        roleButtonRelRepo.save(RoleButtonEntity.builder()
                .buttonId(buttonRepo.findByButtonName("addUser").getButtonId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleButtonRelRepo.save(RoleButtonEntity.builder()
                .buttonId(buttonRepo.findByButtonName("editUser").getButtonId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleButtonRelRepo.save(RoleButtonEntity.builder()
                .buttonId(buttonRepo.findByButtonName("deleteUser").getButtonId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());

        //---------------------------------------------------------------------


        menuRepo.save(MenuEntity.builder()
                .note("主页")
                .routerComponent("DefaultHome")
                .routerName("defaultHome")
                .routerPath("/defaultHome")
                .hasChild(0L)
                .iconType("home")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(1L)
                .menuSequence(1L)
                .parentMenuId(0L)
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("系统设置")
                .routerComponent("")
                .routerName("")
                .routerPath("")
                .hasChild(1L)
                .iconType("setting")
                .isMenu(1L)
                .menuKey("sysManage")
                .menuLevel(1L)
                .menuSequence(2L)
                .parentMenuId(0L)
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("用户管理")
                .routerComponent("UserManage")
                .routerName("UserManage")
                .routerPath("/userManage")
                .hasChild(0L)
                .iconType("user")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(3L)
                .parentMenuId(menuRepo.findByNote("系统设置").getMenuId())
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("角色管理")
                .routerComponent("RoleManage")
                .routerName("RoleManage")
                .routerPath("/roleManage")
                .hasChild(0L)
                .iconType("team")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(4L)
                .parentMenuId(menuRepo.findByNote("系统设置").getMenuId())
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("菜单管理")
                .routerComponent("MenuManage")
                .routerName("MenuManage")
                .routerPath("/menuManage")
                .hasChild(0L)
                .iconType("appstore")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(5L)
                .parentMenuId(menuRepo.findByNote("系统设置").getMenuId())
                .build());
        menuRepo.save(MenuEntity.builder()
                .note("按钮管理")
                .routerComponent("ButtonManage")
                .routerName("ButtonManage")
                .routerPath("/buttonManage")
                .hasChild(0L)
                .iconType("build")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(6L)
                .parentMenuId(menuRepo.findByNote("系统设置").getMenuId())
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("测试1级")
                .routerComponent("")
                .routerName("")
                .routerPath("")
                .hasChild(1L)
                .iconType("radar-chart")
                .isMenu(1L)
                .menuKey("testOne")
                .menuLevel(1L)
                .menuSequence(7L)
                .parentMenuId(0L)
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("测试webSocket")
                .routerComponent("WebSocketTest")
                .routerName("WebSocketTest")
                .routerPath("/webSocketTest")
                .hasChild(0L)
                .iconType("question-circle")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(8L)
                .parentMenuId(menuRepo.findByNote("测试1级").getMenuId())
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("测试TestTwo")
                .routerComponent("TestTwo")
                .routerName("TestTwo")
                .routerPath("/testTwo")
                .hasChild(0L)
                .iconType("question-circle")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(9L)
                .parentMenuId(menuRepo.findByNote("测试1级").getMenuId())
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("测试TestD1")
                .routerComponent("TestD1")
                .routerName("TestD1")
                .routerPath("/testD1")
                .hasChild(0L)
                .iconType("question-circle")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(11L)
                .parentMenuId(menuRepo.findByNote("测试1级").getMenuId())
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("个人中心")
                .routerComponent("Personal")
                .routerName("Personal")
                .routerPath("/personal")
                .hasChild(0L)
                .iconType("smile")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(10L)
                .parentMenuId(menuRepo.findByNote("系统设置").getMenuId())
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("服务器监控")
                .routerComponent("ServerMonitor")
                .routerName("ServerMonitor")
                .routerPath("/serverMonitor")
                .hasChild(0L)
                .iconType("monitor")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(12L)
                .parentMenuId(menuRepo.findByNote("系统设置").getMenuId())
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("日志管理")
                .routerComponent("")
                .routerName("")
                .routerPath("")
                .hasChild(1L)
                .iconType("snippets")
                .isMenu(1L)
                .menuKey("logManage")
                .menuLevel(1L)
                .menuSequence(13L)
                .parentMenuId(0L)
                .build());
        menuRepo.save(MenuEntity.builder()
                .note("登录日志")
                .routerComponent("LoginLog")
                .routerName("LoginLog")
                .routerPath("/loginLog")
                .hasChild(0L)
                .iconType("copy")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(15L)
                .parentMenuId(menuRepo.findByNote("日志管理").getMenuId())
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("操作日志")
                .routerComponent("OperationLog")
                .routerName("OperationLog")
                .routerPath("/operationLog")
                .hasChild(0L)
                .iconType("copy")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(14L)
                .parentMenuId(menuRepo.findByNote("日志管理").getMenuId())
                .build());

        menuRepo.save(MenuEntity.builder()
                .note("m3u8播放器")
                .routerComponent("M3U8Player")
                .routerName("M3U8Player")
                .routerPath("/m3U8Player")
                .hasChild(0L)
                .iconType("copy")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(16L)
                .parentMenuId(menuRepo.findByNote("测试1级").getMenuId())
                .build());
        menuRepo.save(MenuEntity.builder()
                .note("TS播放器")
                .routerComponent("TsPlayer")
                .routerName("TsPlayer")
                .routerPath("/tsPlayer")
                .hasChild(0L)
                .iconType("copy")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(17L)
                .parentMenuId(menuRepo.findByNote("测试1级").getMenuId())
                .build());
        menuRepo.save(MenuEntity.builder()
                .note("mp4播放器")
                .routerComponent("Mp4Player")
                .routerName("Mp4Player")
                .routerPath("/mp4Player")
                .hasChild(0L)
                .iconType("copy")
                .isMenu(1L)
                .menuKey("")
                .menuLevel(2L)
                .menuSequence(20L)
                .parentMenuId(menuRepo.findByNote("测试1级").getMenuId())
                .build());

        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("主页").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("系统设置").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("TS播放器").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("m3u8播放器").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("mp4播放器").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("用户管理").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("角色管理").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("菜单管理").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("按钮管理").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("服务器监控").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());

        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("日志管理").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("操作日志").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());

        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("登录日志").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());

        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("测试1级").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("测试webSocket").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("测试TestTwo").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("个人中心").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("测试TestD1").getMenuId())
                .roleId(roleRepo.findByName("dev").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("主页").getMenuId())
                .roleId(roleRepo.findByName("test").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("测试1级").getMenuId())
                .roleId(roleRepo.findByName("test").getId())
                .build());

        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("主页").getMenuId())
                .roleId(roleRepo.findByName("play").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("测试1级").getMenuId())
                .roleId(roleRepo.findByName("play").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("测试webSocket").getMenuId())
                .roleId(roleRepo.findByName("test").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("测试webSocket").getMenuId())
                .roleId(roleRepo.findByName("play").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("测试TestTwo").getMenuId())
                .roleId(roleRepo.findByName("test").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("系统设置").getMenuId())
                .roleId(roleRepo.findByName("test").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("系统设置").getMenuId())
                .roleId(roleRepo.findByName("play").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("个人中心").getMenuId())
                .roleId(roleRepo.findByName("test").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("测试TestD1").getMenuId())
                .roleId(roleRepo.findByName("test").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("服务器监控").getMenuId())
                .roleId(roleRepo.findByName("test").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("服务器监控").getMenuId())
                .roleId(roleRepo.findByName("play").getId())
                .build());

        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("日志管理").getMenuId())
                .roleId(roleRepo.findByName("test").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("日志管理").getMenuId())
                .roleId(roleRepo.findByName("play").getId())
                .build());

        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("操作日志").getMenuId())
                .roleId(roleRepo.findByName("test").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("操作日志").getMenuId())
                .roleId(roleRepo.findByName("play").getId())
                .build());
        roleMenuRelRepo.save(RoleMenuEntity.builder()
                .menuId(menuRepo.findByNote("登录日志").getMenuId())
                .roleId(roleRepo.findByName("test").getId())
                .build());

        return "init ok";
    }
}
